$(function(){
	var token=localStorage.getItem("token");
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'post',
		url :  server_url+'/rest/tea/info/teaInfoList.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true,
		rowHoverColor:true
	});	
	$("#registerTimeFrom").datetimepicker({
		lang:'ch',
		timepicker:false,
		format:'Y-m-d H:i:s',
		formatDate:'Y-m-d H:i:s',
		maxDate:0,
		onShow:function(){
			this.setOptions({
				maxDate:$("#registerTimeTo").val()?$("#registerTimeTo").val():false
			});
		}
	});
	$("#registerTimeTo").datetimepicker({
		lang:'ch',
		timepicker:false,
		format:'Y-m-d H:i:s',
		formatDate:'Y-m-d H:i:s',
		maxDate:0,
		onShow:function(){
			this.setOptions({
				minDate:$("#registerTimeFrom").val()?$("#registerTimeFrom").val():false
			});
		}
	});
	
	$("#easyDialogNoBtn").click(function(){modifyStaffCancle();});
	//$("#easyDialogYesBtn").click(function(){modifyStaffSave();});
});

function modifyStaffSave(){
	var staff= $("#staffModify").find("option:selected").text();
	$.ajax({
		type : 'GET',
		url : server_url+'/rest/orderManage/modifyStaff.do?token='+token+'&ran='+Math.random(),
		data:{buyerids:ids},
		success : function(data) {
			if (data.retCode == "0000") {
				$("#staff").val(staff);
				closeDialog ();
			}
		}
	});
}


function modifyStaffCancle(){
	closeDialog ();
}

function closeDialog (){
	$('#overlay').css('display','none');
	$('#easyDialogBox').css('display','none');
	$('#easyDialogWrapper').css('display','none');
}

function operate(record, rowIndex, colIndex, options) {
/*	var re = '<a href="javascript:viewBuyer(\'' + gridObj.getRecordIndexValue(record, 'buyerId') + '\');">查看</a>';
	if(record.buyerStatus != '4'){
		re += '<a href="javascript:frozenBuyer(\'' + gridObj.getRecordIndexValue(record, 'buyerId') + '\');">冻结</a>';
	}
    return re;*/
}
// 数据表格参数查询
function doSearch() {
	searchParames = $('#myform').serialize();
    gridObj.search(searchParames);		
	//console.log(searchParames);
}

// 编辑
function editBuyer(id) {
	var url='buyerManage/editBuyer.do?id='+id;
	tl_config.loadToWorkArea(url);
}
function toFrozenBuyer(id) {
	var url='buyerManage/frozenBuyer.do?id='+id;
	tl_config.loadToWorkArea(url);
}
function viewBuyer(id){
	var url='buyerManage/viewBuyer.do?id='+id;
	tl_config.loadToWorkArea(url);
}

function sendMessage(){
	debugger
	var idArr=getCheckedIds();
	var ids=idArr.toString();
	console.log(ids);
	if(!empty(ids)){
		$('#overlay').css('display','block');
		$('#easyDialogBox').css('display','block');
		$('#easyDialogWrapper').css('display','block');
		$("#easyDialogYesBtn").click(function(){
			if("" == $("#staffModify").val() || $("#staffModify").val() == null){
				alert("请输入发送的内容");
				return;
			}
			$.ajax({
				type : 'POST',
				url : server_url+'/rest/tea/user/news/insertUserNews.do?token='+token+'&ran='+Math.random(),
				data:{userids:ids,acceptedNews:$("#staffModify").val()},
				success : function(data) {
					if (data.retCode == "0000") {
						//$("#staff").val(staff);
						closeDialog ();
						alert("发送成功")
					}
				}
			});
		});
	}else{
//		easyDialog.open({
//			  container : {
//				  header : '温馨提示',
//			    content : '请选择要操作的记录！'
//			  },
//			  autoClose : 2000
//			});
		
		alert("请选择要操作的记录！");
	}
}

function sendMessageAll(){
	debugger
	$('#overlay').css('display','block');
	$('#easyDialogBox').css('display','block');
	$('#easyDialogWrapper').css('display','block');
	$("#easyDialogYesBtn").click(function(){
		$.ajax({
			type : 'POST',
			url : server_url+'/rest/tea/user/news/insertUserNews.do?token='+token+'&ran='+Math.random(),
			data:{acceptedNews:$("#staffModify").val()},
			success : function(data) {
				if (data.retCode == "0000") {
					//$("#staff").val(staff);
					closeDialog ();
					alert("发送成功")
				}
			}
		});
	});
}


function getCheckedIds() {
	// values are array
	return gridObj.getCheckedValues('usersid');
}
function delCheckedIds(){
	
	
	
	
	/*
	debugger
	var idArr=getCheckedIds();
	var ids=idArr.toString();
	console.log(ids);
	if(empty(ids)){
		easyDialog.open({
			  container : {
				  header : '温馨提示',
			    content : '请选择要操作的记录！'
			  },
			  autoClose : 2000
			});
		return;
	}
	easyDialog.open({
		  container : {
			  header : '删除确认',
			  content : '您确定要删除这些买家么？',
			  yesFn : function(){
		    	$.ajax({
					url:server_url+'/rest/buyerManage/deleteBuyer.do?ran='+Math.random()+"&token="+token,
					type:'get',
					data:{buyerids:ids},
					success:function(data){
						if(data.retCode=="0000"){
							doSearch();
						}
						else{
							easyDialog.open({
								  container : {
									  header : '提示',
								    content : data.retMsg
								  },
								  autoClose : 2000
								});
							
						} 
						
					}
				})
		    },
		    noFn : true
		  }
		});
*/}
function frozenBuyer(buyerid){
	
	easyDialog.open({
		  container : {
			  header : '冻结确认',
			  content : '您确定要冻结这个买家么？',
			  yesFn : function(){
		    	$.ajax({
					url:server_url+'/rest/buyerManage/checkFrozenBuyer/'+buyerid+'.do?ran='+Math.random()+"&token="+token,
					type:'get',
					success:function(data){
						if(data.retCode=="0000"){
//							doSearch();
							easyDialog.open({
								  container : {
									header : '提示',
								    content : '冻结成功',
								    yesFn:function(){
								    	var url=server_url+'/buyerManage/list.do';
										tl_config.last_url=url;
										tl_config.loadToWorkArea(url);
								    }
								  }
								});
						}
						else{
							easyDialog.open({
								  container : {
									  header : '提示',
								    content : data.retMsg
								  },
								  autoClose : 2000
								});
							
						} 
						
					}
				})
		    },
		    noFn : true
		  }
		});
}